<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item :label="$t('payroll_item_name')" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          :placeholder="$t('please_enter', { name: $t('payroll_item_name') })"
          :show-word-limit="true"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('payroll_item_code')" prop="projectCode">
        <el-input
          v-model="queryParams.projectCode"
          :placeholder="$t('please_enter', { name: $t('payroll_item_code') })"
          :show-word-limit="true"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('project_category')" prop="projectType">
        <el-select
          v-model="queryParams.projectType"
          :placeholder="$t('please_select', { name: $t('project_category') })"
          clearable
        >
          <el-option
            v-for="dict in dict.type.salary_project_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >{{ $t("search") }}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
          $t("reset")
        }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['salary:salaryProject:add']"
          >{{ $t("add") }}</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['salary:salaryProject:export']"
          >{{ $t("export") }}</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <!-- <el-button type="info" plain icon="el-icon-upload" size="mini" v-hasPermi ="['salary:salaryProject:import']"
          >导入</el-button
        > -->
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="salaryProjectList"
      @selection-change="handleSelectionChange"
      border
      :max-height="tableHeight"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        :label="$t('payroll_item_name')"
        align="center"
        prop="projectName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('payroll_item_code')"
        align="center"
        prop="projectCode"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('description_pf_payroll_item')"
        align="center"
        prop="projectDesc"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('project_category')"
        align="center"
        prop="projectType"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.salary_project_type"
            :value="scope.row.projectType"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('update_time')"
        align="center"
        prop="updateTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('modified_by')"
        align="center"
        prop="updateBy"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('action')"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            v-hasPermi="['salary:salaryProject:view']"
            @click="seeUpdate(scope.row)"
            >{{ $t("view") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-hasPermi="['salary:salaryProject:edit']"
            @click="handleUpdate(scope.row)"
            >{{ $t("modify") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-hasPermi="['salary:salaryProject:remove']"
            @click="handleDelete(scope.row)"
            >{{ $t("delete") }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :page-sizes="[10,20,30,50,100, 200]"
    />

    <!-- 添加或修改薪酬项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="180px"
        :disabled="ifDisabled"
      >
        <el-form-item :label="$t('payroll_item_name')" prop="projectName">
          <el-input
            v-model="form.projectName"
            :placeholder="$t('please_enter', { name: $t('payroll_item_name') })"
            :show-word-limit="true"
          />
        </el-form-item>
        <el-form-item :label="$t('payroll_item_code')" prop="projectCode">
          <el-input
            v-model="form.projectCode"
            :placeholder="$t('please_enter', { name: $t('payroll_item_code') })"
            :show-word-limit="true"
          />
        </el-form-item>
        <el-form-item
          :label="$t('description_pf_payroll_item')"
          prop="projectDesc"
        >
          <el-input
            v-model="form.projectDesc"
            type="textarea"
            :placeholder="
              $t('please_enter', { name: $t('description_pf_payroll_item') })
            "
          />
        </el-form-item>
        <el-form-item :label="$t('project_category')" prop="projectType">
          <el-select
            v-model="form.projectType"
            :placeholder="$t('please_enter', { name: $t('project_category') })"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.salary_project_type"
              :key="dict.value"
              :label="dict.label.toString()"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('data_source')" prop="projectDataSource">
          <el-radio-group
            v-model="form.projectDataSource"
            @input="changeProjectDataSource"
          >
            <el-radio
              v-for="dict in dict.type.salary_project_data"
              :key="dict.value"
              :label="dict.value.toString()"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label=""
          prop="projectDataSourceValue"
          v-if="form.projectDataSource == 1"
        >
          <el-input
            v-model="form.projectDataSourceValue"
            type="textarea"
            :placeholder="$t('please_enter', { name: $t('content') })"
          />
        </el-form-item>
        <el-form-item
          label=""
          prop="projectDataSourceValue"
          v-if="form.projectDataSource == 2"
        >
          <el-radio-group v-model="form.projectDataSourceValue">
            <el-radio label="考勤数据的出勤天数">{{
              $t("attendance_ays")
            }}</el-radio>
            <el-radio label="考勤数据的迟到天数">{{
              $t("days_late")
            }}</el-radio>
            <el-radio label="考勤数据的请假天数">{{
              $t("leave_days")
            }}</el-radio>
            <el-radio label="考勤数据的年假天数">{{
              $t("annual_leave_days")
            }}</el-radio>
            <el-radio label="假期数据的假期天数">{{
              $t("holiday_days")
            }}</el-radio>
            <el-radio label="假期数据的年假天数">{{
              $t("holiday_annual_leave_days")
            }}</el-radio>
            <el-radio label="固定薪资">{{ $t("fixed_salary") }}</el-radio>
            <el-radio label="临时薪资">{{
              $t("temporary_compensation")
            }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label=""
          prop="projectDataSourceValue"
          v-if="form.projectDataSource == 3"
        >
          <el-input
            v-model="form.projectDataSourceValue"
            type="textarea"
            :placeholder="$t('please_enter', { name: $t('content') })"
            readonly
            @click.native="showGsOpen"
          />
        </el-form-item>

        <el-form-item :label="$t('hong_kong_taxable_item')" prop="levyDuty">
          <el-radio-group v-model="form.levyDuty">
            <el-radio :label="'1'">{{ $t("yes") }}</el-radio>
            <el-radio :label="'0'">{{ $t("no") }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          :label="$t('hong_kong_tax_code')"
          prop="levyDutyCode"
          v-if="form.levyDuty == 1"
        >
          <el-select
            v-model="form.levyDutyCode"
            :placeholder="
              $t('please_select', { name: $t('hong_kong_tax_code') })
            "
          >
            <el-option
              v-for="dict in dict.type.levy_duty_code"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="addOrSubtract">
          <el-radio-group v-model="form.addOrSubtract">
            <el-radio :label="'2'">{{ $t("salary_increment") }}</el-radio>
            <el-radio :label="'1'">{{ $t("salary_deduction") }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          :label="$t('mandatory_provident_fund_related_income')"
          prop="mandatory"
        >
          <el-radio-group v-model="form.mandatory">
            <el-radio :label="'1'">{{ $t("yes") }}</el-radio>
            <el-radio :label="'0'">{{ $t("no") }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          :label="$t('voluntary_contribution_related_income')"
          prop="installment"
        >
          <el-radio-group v-model="form.installment">
            <el-radio :label="'1'">{{ $t("yes") }}</el-radio>
            <el-radio :label="'0'">{{ $t("no") }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          :label="$t('provident_fund_related_income')"
          prop="accumulationFund"
        >
          <el-radio-group v-model="form.accumulationFund">
            <el-radio :label="'1'">{{ $t("yes") }}</el-radio>
            <el-radio :label="'0'">{{ $t("no") }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('belongs_to_salary')" prop="salary">
          <el-radio-group v-model="form.salary">
            <el-radio :label="'1'">{{ $t("yes") }}</el-radio>
            <el-radio :label="'0'">{{ $t("no") }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          :label="$t('mandatory_provident_fund')"
          prop="mandatoryFund"
        >
          <el-tooltip
            class="item"
            style="margin-right: 20px"
            effect="light"
            :content="$t('voluntary_contribution_will_not_be_calculated')"
            placement="right-start"
          >
            <i class="el-icon-question" />
          </el-tooltip>
          <el-radio-group v-model="form.mandatoryFund">
            <el-radio :label="'1'">{{ $t("yes") }}</el-radio>
            <el-radio :label="'0'">{{ $t("no") }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          :label="$t('display_x_decimal_places')"
          prop="decimalPoint"
        >
          <el-input-number
            v-model="form.decimalPoint"
            :min="0"
            :max="9"
            :placeholder="
              $t('please_enter', { name: $t('display_x_decimal_places') })
            "
            :show-word-limit="true"
          />
        </el-form-item>
        <el-form-item :label="$t('rounding_rules')" prop="carryRule">
          <el-select
            v-model="form.carryRule"
            :placeholder="$t('please_select', { name: $t('rounding_rules') })"
          >
            <el-option
              v-for="dict in dict.type.carry_rule"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="submitForm"
          v-if="ifDisabled == false"
          >{{ $t("confirm") }}</el-button
        >
        <el-button @click="cancel">{{ $t("cancel") }}</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="$t('formula_setting')"
      :visible.sync="gsOpen"
      width="800px"
      append-to-body
    >
      <gsEditor
        v-model="gsText"
        @gsCancel="gsCancel"
        @gsSubmitForm="gsSubmitForm"
      ></gsEditor>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSalaryProject,
  listAllSalaryProject,
  getSalaryProject,
  delSalaryProject,
  addSalaryProject,
  updateSalaryProject,
} from "@/api/salary/salaryProject";
import gsEditor from "./gsEditor";
export default {
  name: "SalaryProject",
  dicts: [
    "carry_rule",
    "levy_duty_code",
    "salary_project_type",
    "salary_project_data",
  ],
  components: {
    gsEditor,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 薪酬项目表格数据
      salaryProjectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        projectCode: null,
        projectType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      ifDisabled: false,
      gsOpen: false,
      gsText: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询薪酬项目列表 */
    getList() {
      this.loading = true;
      listSalaryProject(this.queryParams).then((response) => {
        this.salaryProjectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectName: null,
        projectCode: null,
        projectDesc: null,
        projectType: null,
        projectDataSource: null,
        projectDataSourceValue: null,
        levyDuty: null,
        levyDutyCode: null,
        addOrSubtract: null,
        mandatory: null,
        installment: null,
        accumulationFund: null,
        salary: null,
        mandatoryFund: null,
        decimalPoint: null,
        carryRule: null,
        updateTime: null,
        updateBy: null,
        createTime: null,
        createBy: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.ifDisabled = false;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 修改数据来源
    changeProjectDataSource(e) {
      this.form.projectDataSourceValue = null;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.ifDisabled = false;
      this.open = true;
      this.title = this.$t("add_payroll_item");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSalaryProject(id).then((response) => {
        this.form = response.data;
        this.ifDisabled = false;
        this.open = true;
        this.title = this.$t("modify");
      });
    },
    /** 查看按钮操作 */
    seeUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSalaryProject(id).then((response) => {
        this.form = response.data;
        this.ifDisabled = true;
        this.open = true;
        this.title = this.$t("view");
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateSalaryProject(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t("operation_successful"));
              this.open = false;
              this.getList();
            });
          } else {
            addSalaryProject(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t("operation_successful"));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(this.$t("delete_data"))
        .then(function () {
          return delSalaryProject(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t("operation_successful"));
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "salary/salaryProject/export",
        {
          ...this.queryParams,
        },
        `${this.$t("payroll_items_list")}_${new Date().getTime()}.xlsx`
      );
    },
    showGsOpen() {
      this.gsText = this.form.projectDataSourceValue;
      this.gsOpen = true;
    },
    gsCancel() {
      this.gsOpen = false;
    },
    gsSubmitForm() {
      this.gsOpen = false;
      this.form.projectDataSourceValue = this.gsText;
    },
  },
};
</script>

<style scoped>
.el-radio-button__inner,
.el-radio-group {
  line-height: inherit;
}
</style>
